package miaodi

import (
	"fmt"
	"os"
	"github.com/PuerkitoBio/goquery"
	"bufio"
	"log"
	//"strings"
	//"encoding/csv"
	"github.com/tealeg/xlsx"
	//"go/doc"
)

var Header = []string{
	"开发者邮箱",
	"计费类型",
	"提交API失败数量（条）",
	"提交号码数",
	"提交网关失败数",
	"发送成功数",
	"发送失败数",
	"未知状态数",
	"应计费数",
	"单位价格",
	"消费总费用"}

type info struct {
	email         string
	costType      string
	apiFail       string
	commitCount   string
	getawayFail   string
	sendSuccess   string
	sendFail      string
	unknownStatus string
	costCount     string
	price         string
	totalCost     string
}

var ItemInfo [][]string

func LoadFile(filename string) *os.File {
	f, err := os.Open(filename)
	if err != nil {
		if os.IsNotExist(err) {
			log.Fatal(err)
			fmt.Println("File not exist")
		}
	}
	if err != nil {
		fmt.Println(err)
	}
	return f
}

func QueryHtml(file *os.File) *goquery.Document {
	reader := bufio.NewReader(file)
	doc, err := goquery.NewDocumentFromReader(reader)
	if err != nil {
		log.Fatal(err)
	}
	return doc
}

func ParseHtml(doc *goquery.Document) [][]string {
	ItemInfo = make([][]string, 0)

	doc.Find("tbody tr").Each(func(i int, selection *goquery.Selection) {
		email := selection.Find("td").Eq(0).Text()
		costType := selection.Find("td").Eq(1).Text()
		apiFail := selection.Find("td").Eq(2).Text()
		commitCount := selection.Find("td").Eq(3).Text()
		getawayFail := selection.Find("td").Eq(4).Text()
		sendSuccess := selection.Find("td").Eq(5).Text()
		sendFail := selection.Find("td").Eq(6).Text()
		unknownStatus := selection.Find("td").Eq(7).Text()
		costCount := selection.Find("td").Eq(8).Text()
		price := selection.Find("td").Eq(9).Text()
		totalCost := selection.Find("td").Eq(10).Text()

		str := []string{
			email,
			costType,
			apiFail,
			commitCount,
			getawayFail,
			sendSuccess,
			sendFail,
			unknownStatus,
			costCount,
			price,
			totalCost}

		ItemInfo = append(ItemInfo, str)
	})

	return ItemInfo
}

//func AddHeader(sheet *xlsx.Sheet, document *goquery.Document) {
//	row := (*sheet).AddRow()
//
//	document.Find("thead tr").Each(func(i int, selection *goquery.Selection) {
//		length := selection.Find("th").Length()
//		for i := 0; i < length; i++ {
//			cell := row.AddCell()
//			cell.Value = selection.Find("th").Eq(i).Text()
//		}
//	})
//}

func OpenFile() {
	name := os.Getenv("GOPATH") + "/src/miaodi/miaodi.html"
	f, err := os.Open(name)
	if err != nil {
		if os.IsNotExist(err) {
			log.Fatal(err)
			fmt.Println("File not exist")
		}
	}
	defer f.Close()
	if err != nil {
		fmt.Println(err)
		return
	}

	reader := bufio.NewReader(f)
	doc, err := goquery.NewDocumentFromReader(reader)
	if err != nil {
		log.Fatal(err)
	}

	//ItemInfo := make([]*info, 0)
	var ItemInfo [][]string

	ItemInfo = append(ItemInfo, Header)
	xl := xlsx.NewFile()
	sheet, err := xl.AddSheet("Sheet1")
	addHeader(sheet, doc)
	doc.Find("tbody tr").Each(func(i int, selection *goquery.Selection) {
		email := selection.Find("td").Eq(0).Text()
		costType := selection.Find("td").Eq(1).Text()
		apiFail := selection.Find("td").Eq(2).Text()
		commitCount := selection.Find("td").Eq(3).Text()
		getawayFail := selection.Find("td").Eq(4).Text()
		sendSuccess := selection.Find("td").Eq(5).Text()
		sendFail := selection.Find("td").Eq(6).Text()
		unknownStatus := selection.Find("td").Eq(7).Text()
		costCount := selection.Find("td").Eq(8).Text()
		price := selection.Find("td").Eq(9).Text()
		totalCost := selection.Find("td").Eq(10).Text()

		row := sheet.AddRow()

		cell := row.AddCell()
		cell.Value = email

		cell = row.AddCell()
		cell.Value = costType

		cell = row.AddCell()
		cell.Value = apiFail
		cell.FormattedValue()

		cell = row.AddCell()
		cell.Value = commitCount
		cell.FormattedValue()

		cell = row.AddCell()
		cell.Value = getawayFail
		cell.FormattedValue()

		cell = row.AddCell()
		cell.Value = sendSuccess
		cell.FormattedValue()

		cell = row.AddCell()
		cell.Value = sendFail
		cell.FormattedValue()

		cell = row.AddCell()
		cell.Value = unknownStatus
		cell.FormattedValue()

		cell = row.AddCell()
		cell.Value = costCount

		cell = row.AddCell()
		cell.Value = price

		cell = row.AddCell()
		cell.Value = totalCost

		//str := []string{
		//	email,
		//	costType,
		//	apiFail,
		//	strings.Split(commitCount,"/")[0],
		//	strings.Split(getawayFail,"/")[0],
		//	strings.Split(sendSuccess, "/")[0],
		//	strings.Split(sendFail, "/")[0],
		//	strings.Split(unknownStatus,"/")[0],
		//	costCount,
		//	price,
		//	totalCost}

		//str := []string{
		//	email,
		//	costType,
		//	"'"+apiFail,
		//	"'"+commitCount,
		//	"'"+getawayFail,
		//	"'"+sendSuccess,
		//	"'"+sendFail,
		//	"'"+unknownStatus,
		//	costCount,
		//	price,
		//	totalCost}

		//ItemInfo = append(ItemInfo, str)
		//in := new(info)
		//in.email = email
		//in.costType = costType
		//in.apiFail = apiFail
		//in.commitCount = commitCount
		//in.getawayFail = getawayFail
		//in.sendSuccess = sendSuccess
		//in.sendFail = sendFail
		//in.unknownStatus = unknownStatus
		//in.costCount = costCount
		//in.price = price
		//in.totalCost = totalCost
		//ItemInfo = append(ItemInfo, in)
	})
	xl.Save(os.Getenv("GOPATH") + "/qq.xlsx")

	//file,err := os.Create(os.Getenv("GOPATH") + "/qq.csv")
	//
	//if err != nil {
	//	fmt.Println(err)
	//}
	//file.WriteString("\xEF\xBB\xBF")
	//
	//wq := csv.NewWriter(file)
	//wq.WriteAll(ItemInfo)
	//wq.Flush()
}
